package cn.shentianlan.service.impl;

import cn.shentianlan.criteria.ClassroomSchedulingCriteria;
import cn.shentianlan.domain.dto.ClassroomSchedulingDTO;
import cn.shentianlan.domain.entity.ClassroomScheduling;
import cn.shentianlan.mapper.ClassroomSchedulingMapper;
import cn.shentianlan.service.ClassroomSchedulingService;
import cn.shentianlan.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 陈俊源
 * @Description TODO
 * @Date 2021/5/6
 * @Version 1.0
 */
@Service
public class ClassroomSchedulingServiceImpl implements ClassroomSchedulingService {

    @Autowired
    private ClassroomSchedulingMapper classroomSchedulingMapper;

    @Override
    public Result findClassroomSchedulingByCriteria(ClassroomSchedulingCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize(), false);
        List<ClassroomSchedulingDTO> classroomSchedulingDTOList = classroomSchedulingMapper.findClassroomSchedulingByCriteria(criteria);
        Integer count = classroomSchedulingMapper.findCountByCriteria(criteria);
        PageInfo<ClassroomSchedulingDTO> pageInfo = new PageInfo<>(classroomSchedulingDTOList);
        pageInfo.setTotal(count);
        return Result.success(pageInfo);
    }

    @Override
    public Result saveOrUpdate(ClassroomSchedulingDTO classroomSchedulingDTO) {
        if (classroomSchedulingDTO == null) {
            return Result.fail("参数对象不能为空");
        }
        if (classroomSchedulingDTO.getClassroomId() == null) {
            return Result.fail("课室id为空，无法进行保存操作");
        }
        ClassroomSchedulingCriteria classroomSchedulingCriteria = new ClassroomSchedulingCriteria();
        Map<String,Object> args = new HashMap<String, Object>();
        args.put("classroomId",classroomSchedulingDTO.getClassroomId());
        args.put("id",classroomSchedulingDTO.getId());
        classroomSchedulingCriteria.setClassroomId(classroomSchedulingDTO.getClassroomId());
        List<ClassroomSchedulingDTO> classroomSchedulingDTOList = classroomSchedulingMapper.checkClassroomSchedulingByMap(args);
        for (ClassroomSchedulingDTO dto : classroomSchedulingDTOList) {
            boolean status = (classroomSchedulingDTO.getStartTime().isBefore(dto.getEndTime()) || classroomSchedulingDTO.getStartTime().isEqual(dto.getEndTime()))
                    && (classroomSchedulingDTO.getEndTime().isEqual(dto.getStartTime()) ||classroomSchedulingDTO.getEndTime().isAfter(dto.getStartTime()) );
            if (status){
                return Result.fail(dto,"检测到课室时间安排出现冲突");
            }
        }
        if (classroomSchedulingDTO.getId() == null){
            classroomSchedulingMapper.save(classroomSchedulingDTO);
        }else{
            classroomSchedulingMapper.update(classroomSchedulingDTO);
        }
        return Result.success("保存成功");
    }

    @Override
    public Result getClassrooomSchedulingById(Long id) {
        if (id==null){
            return Result.fail("id为空，无法获取课室安排信息");
        }
        ClassroomScheduling classroomScheduling = classroomSchedulingMapper.getClassroomSchedulingById(id);
        return Result.success(classroomScheduling);
    }

    @Override
    public Result deleteCLassroomSchedulingById(Long id) {
        if (id==null) {
            return Result.fail("id为空，无法获取课室安排信息");
        }
        ClassroomScheduling classroomScheduling = classroomSchedulingMapper.getClassroomSchedulingById(id);
        if (classroomScheduling==null){
            return Result.fail("无法找到该id对应的课室安排信息，删除操作失败");
        }
        classroomSchedulingMapper.deleteSchedulingById(id);
        return Result.success();
    }

    /**
     * 根据课室id，开始使用时间和结束使用时间查询到该课室对应安排信息
     * @param classroomId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public ClassroomScheduling getClassrooomSchedulingByClassroomIdAndStartTimeAndEndTime(Long classroomId, LocalDateTime startTime, LocalDateTime endTime) {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("classroomId",classroomId);
        args.put("startTime",startTime);
        args.put("endTime",endTime);
        ClassroomScheduling classrooomScheduling = classroomSchedulingMapper.getClassrooomSchedulingByClassroomIdAndStartTimeAndEndTime(args);
        return classrooomScheduling;
    }
}
